package org.bemo.utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

/**
 * 解析工具类
 */
public class JXUtils {
	
	
	
	/**
	 * 47解析vip地址
	 */
	private static final String VIP = "vip.api.47ks.com";
	
	/**
	 * 47解析接口地址
	 */
	private static final String APIURL = "https://vip.api.47ks.com/cp0309/?v=";

	/**
	 * 获取ajax返回信息正则
	 */
	private static final String AJAXPATTERN = "get\\(\"(.*?)\"";

	/**
	 * 47解析VIP站点白名单接口
	 */
	private static final String VIPURL = "http://www.gijepyhi.com/gije1.php?url=";

	/**
	 * 47解析获取视频解析真实解析地址
	 */
	private static final String REALURL = "https://vip.api.47ks.com/config/webmain.php";
	
	/**
	 * MD5加密前缀
	 */
	private static final String MD5 = "TOYONG";

	
	
	/**
	 * 解析视频地址：调用47解析接口 参数1：视频地址 返回：接口返回的Json对象
	 */
	public static JSONObject getJsonObject(String videoUrl) throws Exception {
		
		// 获取请求地址
		String getAjax = APIURL + videoUrl;
		String ajaxResultHtml = HttpUtil.sendGet(getAjax, null);

		// 创建 Pattern 对象
		Pattern r = Pattern.compile(AJAXPATTERN);
		Matcher m = r.matcher(ajaxResultHtml);
		String token = null;
		if (m.find()) {
			String[] split = m.group(0).split("\"");
			System.out.println(Arrays.toString(split));
			token = JsUtil.invk(split[1]);
			System.out.println(token);
		} else {
			System.out.println("NO MATCH");
		}

		String parm = "token=" + token + "&csign=7F88C42258232C6199FBAA3F6AE7C08E&v=" + videoUrl
				+ "&cip=&from="+REALURL + videoUrl + "&t="+videoUrl+"&up=0&ptype=";
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("Host", VIP);
		map.put("Accept", "application/json, text/javascript, */*; q=0.01");
		map.put("X-Requested-With", "XMLHttpRequest");
		map.put("Cache-Control", "no-cache");
		map.put("Referer", APIURL + videoUrl);
		map.put("Content-Type", "application/x-www-form-urlencoded");// 必加！！！！

		String string = HttpRequestUtils.httpPost(parm, REALURL, map);
		// 转换json
		JSONObject jsonObject = JSONObject.fromObject(string);
		
		/*
		 * String success = jsonObject.getString("success"); if (success == null
		 * || "0".equals(success)) { System.out.println("解析失败"); return null; }
		 * // 得到解析后的Url开始请求 String play = jsonObject.getString("play"); String
		 * url = jsonObject.getString("url"); System.out.println(play);
		 * System.out.println(url); Map<String, String> map2 = new
		 * HashMap<String, String>(); map2.put("Referer",
		 * "https://vip.api.47ks.com/cp0309/?v=https://v.qq.com/x/cover/ttqxo9s3cblgofl.html"
		 * );
		 * 
		 * String xml = HttpRequestUtils.httpPost("", url, map2);
		 * System.out.println(xml);
		 */
		return jsonObject;

	}
	
	/**
	 * 获取视频解析key
	 * @throws Exception 
	 */
	public static String getVkey(String videoUrl) throws Exception{
		JSONObject jsonObject = getJsonObject(videoUrl);
		Map<String, Object> cacheMap = MapDataCache.getCacheMap();
		String key = MD5Utils.md5(MD5+videoUrl);
		cacheMap.put(key, jsonObject);
		return key;
	}

	public static JSONObject getJson(String vkey) throws Exception {
		Map<String, Object> cacheMap = MapDataCache.getCacheMap();
		JSONObject jsonObject = (JSONObject) cacheMap.get(vkey);
		//处理json对象数据请求地址
		//创建新的，不能修改旧的！！
		JSONObject jsonObject2 = new JSONObject();
		jsonObject2.put("success", jsonObject.get("success"));
		jsonObject2.put("play",jsonObject.get("play"));
		//jsonObject2.put("url", "http://localhost:9000/getXml/"+vkey);//resf
		jsonObject2.put("url", "/getXml?vkey="+vkey);//手动加参数
		System.out.println(jsonObject.toString());
		return jsonObject2;
	}

	public static String getXml(String vkey) throws Exception {
		Map<String, Object> cacheMap = MapDataCache.getCacheMap();
		JSONObject jsonObject = (JSONObject) cacheMap.get(vkey);
		//请求并解析XML
		//得到解析后的Url开始请求
		String play = jsonObject.getString("play");
		String url = jsonObject.getString("url");
		System.out.println(play);
		System.out.println(url);
		Map<String, String> map2 = new HashMap<String, String>(); 
		map2.put("Referer", "https://vip.api.47ks.com/cp0309/?v=https://v.qq.com/x/cover/ttqxo9s3cblgofl.html");
		String xml = HttpRequestUtils.httpPost("", url, map2);
		//处理xml
		xml = xml.replace("47影视云解析服务","测试解析服务");
		xml = xml.replaceAll("<flashvars>(.*?)</flashvars>", "");
		return xml;
	}
	
	

}
